/**
 * Copyright � 2012 GestureSoft. ALL RIGHTS RESERVED.
 * This file contains proprietary and GestureSoft CONFIDENTIAL Information.
 * Use, disclosure or reproduction is prohibited.
 * 
 * @fileName TwitterStageWebView.as
 * @creationDate Aug 8, 2012
 * @author 
 * @version 1.0
 */
package com.gesturesoft.imultimax.socialize.twitter
{
	import com.gesturesoft.imultimax.socialize.org.osflash.signals.Signal;
	
	import flash.display.Stage;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.LocationChangeEvent;
	import flash.events.StageOrientationEvent;
	import flash.geom.Rectangle;
	import flash.media.StageWebView;
	
	import mx.core.UIComponent;
	
	/**
	 * 
	 * @author mike britton
	 * 
	 * Subclass UIComponent so it can mediate
	 * with StageWebView through composition
	 * and be added to the parent since
	 * UIComponent implements IVisualElement.
	 */
	public class TwitterStageWebView extends UIComponent
	{
		private var stageWebView:StageWebView;
		private var myStage:Stage;

		private var __url:String;
		private var __text:String;
		private var __yOffset:int = 0;
		private var __heightOffset:int = 0;
		private var __xOffset:int = 0;
		//private var __twitterService:TwitterService;
		
		public var locationChangingSignal:Signal = new Signal();
		public var locationChangeSignal:Signal = new Signal();
		public var locationCompleteSignal:Signal = new Signal();
		public var accessTokenReceived:Signal = new Signal();
		public var failureSignal:Signal = new Signal();
		
		public function TwitterStageWebView()
		{
			addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
		}
		
		
		public function hide():void
		{
			
			stageWebView.stage = null;
			
		}
		
		public function show():void
		{
			
			stageWebView.stage = myStage;
			
		}
		
		public function dispose():void
		{
			
			hide();
			stageWebView.dispose();
			
		}
		
		protected function addedToStageHandler(event:Event):void
		{
			
			myStage = event.currentTarget.document.stage;
			removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
			myStage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE,onOrientationChange);
			measureBox();
			hide();
			
		}
		
		protected function measureBox():void
		{
			
			stageWebView = new StageWebView();
			stageWebView.stage = myStage;
			stageWebView.viewPort = new Rectangle(xOffset, yOffset, myStage.fullScreenWidth-xOffset, myStage.fullScreenHeight-heightOffset);
			stageWebView.addEventListener(ErrorEvent.ERROR, errorHandler);
			stageWebView.addEventListener(LocationChangeEvent.LOCATION_CHANGE, locationChangeHandler);
			stageWebView.addEventListener(LocationChangeEvent.LOCATION_CHANGING, locationChangingHandler);
			stageWebView.addEventListener(Event.COMPLETE, locationCompleteHandler);
			
			if (__url)
			{
				
				stageWebView.loadURL(__url);
				
			} 
			else if (__text) 
			{
				
				stageWebView.loadString(__text);
				
			}
			
		}
		
		protected function onOrientationChange(event:StageOrientationEvent):void
		{
			if(stage)
			{
				
				callLater(resize);
				
			}
			
		}
		
		protected function resize():void
		{
			
			stageWebView.viewPort = new Rectangle(0, yOffset, myStage.fullScreenWidth, myStage.fullScreenHeight - heightOffset);
			
		}
		
		protected function locationChangingHandler(event:LocationChangeEvent):void
		{
			
			if(event.location == "https://api.twitter.com/oauth/authorize")
			{
				
				locationChangingSignal.dispatch(event.clone());
				
			}
			
		}
		
		protected function locationCompleteHandler(event:Event):void
		{
			
			locationCompleteSignal.dispatch(event.clone());
			
		}
		
		protected function locationChangeHandler(event:LocationChangeEvent):void
		{
			
			trace(event.location);
			if(event.location == "https://api.twitter.com/oauth/authorize")
			{
				
				accessTokenReceived.dispatch();
				
			}
			else
			{
				
				locationChangeSignal.dispatch(event.clone());
				
			}
			
		}
		
		protected function errorHandler(event:Event):void
		{
			
			failureSignal.dispatch(event);
			
		}
		
		public function get xOffset():Number
		{
			
			return __xOffset;
			
		}
		
		public function set xOffset(val:Number):void
		{
			
			__xOffset = val;
			
		}
		
		public function get yOffset():Number
		{
			
			return __yOffset;
			
		}
		
		public function set yOffset(val:Number):void
		{
			
			__yOffset = val;
			
		}
		
		public function get heightOffset():Number
		{
			
			return __heightOffset;
			
		}
		
		public function set heightOffset(val:Number):void
		{
			
			__heightOffset = val;
			
		}
		
		public function set url(url:String):void
		{
			
			__url = url;
			
			if (stageWebView)
			{
				
				stageWebView.loadURL(url);
				
			}
			
		}
		
		public function set text(text:String):void
		{
			
			__text = text;
			
			if (stageWebView)
			{
				
				stageWebView.loadString(text);
				
			}
			
		}
		
	}
	
}